package org.xx.armory.swing.components.formatters;

import javax.swing.text.DefaultFormatter;
import java.text.ParseException;

import static org.xx.armory.commons.SysUtils.removeNonDigit;
import static org.xx.armory.swing.components.FormatUtils.getFloatFormat;

public class FloatFormatter
        extends DefaultFormatter {
    //        extends AbstractTextFormatter {
    public FloatFormatter(
//            String prefix,
//            String suffix
    ) {
//        super(prefix, suffix);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Object stringToValue(
            String text
    )
            throws ParseException {
        try {
            return Double.parseDouble(removeNonDigit(text));
        } catch (NumberFormatException ignore) {
            return null;
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String valueToString(
            Object value
    ) {
        if (value instanceof Number) {
            return getFloatFormat().format(value);
        }
        return "";
    }
}
